$(function(){
	function delImg(e){
		e.stopPropagation();
		$(this).parent().animate({width:0,height:0},400,function(){
			$(this).remove();
		});
	}
	
	$('.daoshi-page-name').click(function(){
		$(this).find('.daoshi-choose').slideToggle('fast');
	});
	$('.daoshi-choose-item').click(function(e){
		e.stopPropagation();
		$(this).addClass('active').siblings().removeClass('active');
	});
	$('.icon-daoshi-minus').on('click',delImg);
	$('.daoshi-fabu-add').eq(0).click(function(){
		$('.daoshi-fabu-content').append('<p class="daoshi-content-text"><input type="text" class="form-control" name="message[]" placeholder="请输入文字描述"><input type="hidden" name="msgtype[]" value="text"><span class="icon-daoshi-minus"></span></p>').find('.icon-daoshi-minus').on('click',delImg);
	});
	$('.daoshi-fabu-add').eq(1).click(function(){
		$('.daoshi-fabu-content').append('<div class="add-daoshi-info">' +
					'<span class="icon-daoshi-minus"></span>' +
					'<div class="ub ub-ver ub-ac poblish-img-addtxt">' +
						'<span class="icon-daoshi-plus"></span>' +
						'<p>添加图片</p>' +
					'</div>' +
					'<input class="pic-add" type="hidden" name="message[]" value="">' + 
					'<input type="hidden" name="msgtype[]" value="pic">' +
				'</div>').find('.icon-daoshi-minus').on('click',delImg);
	});
	
	$('.teach-add').click(function(e){
		e.preventDefault();
		var _this = $(this);
		var type = _this.attr('data-type');
		if(type == 'yes')
		{
			var url = $('.teachForm').attr('data-url');
			if(url == undefined || url == '')
			{
				showToast('配置错误');
				return false;
			}
			var data = getPostData('teachForm');
			_this.attr('data-type', 'no');
			$.ajax({
				url: url,
				type: 'POST',
				dataType: 'json',
				data: data,
				success: function(res){
					showToast(res.info);
					if(res.status == 1 && res.url != '')
					{
						setTimeout(function(){
							location.href = res.url
						}, 3000);
						_this.attr('data-type', 'done');
					}
					else
					{
						_this.attr('data-type', 'yes');
					}
					
				},
				error: function(res){
					showToast('系统繁忙');
					_this.attr('data-type', 'yes');
				}
			});
		}
		else if(type == 'done')
		{
			showToast('数据已提交,请稍后');
		}
		else
		{
			showToast('数据提交中');
		}
	});
	
	var num = 0;
	var limitNum = 1;
	var stepImages = {
			localId: [],
			serverId: []
	};
	
	$('.daoshi-fabu-content').on('click', '.add-daoshi-info', function(e){
		e.preventDefault();
		var _this = $(this);
		wx.chooseImage({
			count: 1,
			success: function(res)
			{
				stepImages.localId = res.localIds;
		        if (stepImages.localId.length == 0) {
		  	      	showToast('请先选择图片');
					return;
		  	    }
		        var i = 0; 
		        var length = stepImages.localId.length;
		        stepImages.serverId = [];
		        
		        function upload() 
		        {
					var img;
		        	wx.uploadImage({
		        		localId: stepImages.localId[i],
		      		    success: function (res) {
		      		    	i++;
		      		    	num++;
		      		    	stepImages.serverId.push(res.serverId);
		      		    	img = res.serverId;
		      		    	_this.css({
		      		    		'background': 'url(' + stepImages.localId[i-1] + ')',
		      		    		'background-size': 'cover',
		      		    		'background-repeat': 'no-repeat',
		      		    		'background-position': 'center'
		      		    	}).find('.pic-add').val(img);
		      		    	_this.find('.poblish-img-addtxt').remove();
		      		    	if(num>=limitNum)
							{
								return;
							}
		      		    	if(i<length)
		      		    	{
		      		    		upload();
		      		    	}
		      		    }
		        	});
		        }
		        upload();
			}
		});
	});
});